package com.biostime.qdingding.modules.qd.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.biostime.qdingding.common.config.Global;
import com.biostime.qdingding.common.persistence.Page;
import com.biostime.qdingding.common.utils.StringUtils;
import com.biostime.qdingding.common.web.BaseController;
import com.biostime.qdingding.modules.qd.entity.Comment;
import com.biostime.qdingding.modules.qd.service.CommentService;

/**
 * 评论Controller
 * @author king
 * @version 2015-11-05
 */
@Controller
@RequestMapping(value = "${adminPath}/qd/comment")
public class CommentController extends BaseController {

	@Autowired
	private CommentService commentService;
	
	@ModelAttribute
	public Comment get(@RequestParam(required=false) String id) {
		Comment entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = commentService.get(id);
		} 
		if (entity == null){
			entity = new Comment();
		}
		return entity;
	}
	
	@RequiresPermissions("qd:comment:edit")
	@ResponseBody
	@RequestMapping(value = "audit")
	public String audit(Comment comment, Model model) {
		//model.addAttribute("comment", comment);
		
		String result = "{\"errCode\":\"%s\",\"errMsg\":\"%s\"}";
		String code = "0",msg = "屏蔽成功";
		
		try{
			if(comment.getIsAudit()==0){
				comment.setIsAudit(1);
			}
			else{
				comment.setIsAudit(0);
				msg = "已取消屏蔽";
			}
			
			boolean res = commentService.audit(comment);
			if(!res){
				code = "1";
				msg = "屏蔽失败";
			}
		}catch(Exception ex){
			code = "1";
			msg = ex.getMessage();
		}
		
		return String.format(result, code, msg);
	}
	
	@RequiresPermissions("qd:comment:view")
	@RequestMapping(value = {"list", ""})
	public String list(Comment comment, HttpServletRequest request, HttpServletResponse response, Model model) {
		comment.setCategory("0");
		Page<Comment> page = commentService.findPage(new Page<Comment>(request, response), comment); 
		model.addAttribute("page", page);
		return "modules/qd/commentList";
	}

	@RequiresPermissions("qd:comment:view")
	@RequestMapping(value = "form")
	public String form(Comment comment, Model model) {
		model.addAttribute("comment", comment);
		return "modules/qd/commentForm";
	}

	@RequiresPermissions(value={"qd:comment:edit","qd:comment:add"}, logical=Logical.OR)
	@RequestMapping(value = "save")
	public String save(Comment comment, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, comment)){
			return form(comment, model);
		}
		commentService.save(comment);
		addMessage(redirectAttributes, "保存成功");
		return "redirect:"+Global.getAdminPath()+"/qd/comment/?repage";
	}
	
	@RequiresPermissions("qd:comment:delete")
	@RequestMapping(value = "delete")
	public String delete(@RequestParam(value="ids",required=false) String idsRequest, Comment comment, RedirectAttributes redirectAttributes) {
		//批量删除
		if(!StringUtils.isEmpty(idsRequest)){
			String[] ids = idsRequest.split(",");
			for(String id : ids){
				commentService.delete(new Comment(id));
			}
		}else{
			commentService.delete(comment);
		}
		
		addMessage(redirectAttributes, "删除成功");
		return "redirect:"+Global.getAdminPath()+"/qd/comment/?repage";
	}

}